/**
 * homeWorkManagement.js
 * 作业管理reducer
 *
 * created by xurui
 * version 1.0.1
 * 2016年04月18日
 */
import { ASSIGNMENT, CORRECTION } from '../../../constants/homeWorkManagement';
import {
		SWITCH_HOME_WORK_VIEW,
		GET_ASSIGNMENT_TABLE_REQUEST,
		GET_ASSIGNMENT_TABLE_SUCCESS,
		GET_ASSIGNMENT_TABLE_FAILURE,

		GET_ASSIGNMENT_CHARTS_FREQUENCY_REQUEST,
		GET_ASSIGNMENT_CHARTS_FREQUENCY_SUCCESS,
		GET_ASSIGNMENT_CHARTS_FREQUENCY_FAILURE,

		GET_ASSIGNMENT_CHARTS_AVERAGE_REQUEST,
		GET_ASSIGNMENT_CHARTS_AVERAGE_SUCCESS,
		GET_ASSIGNMENT_CHARTS_AVERAGE_FAILURE,

		GET_ASSIGNMENT_CHARTS_DIFFICULTY_REQUEST,
		GET_ASSIGNMENT_CHARTS_DIFFICULTY_SUCCESS,
		GET_ASSIGNMENT_CHARTS_DIFFICULTY_FAILURE,

		GET_ASSIGNMENT_CHARTS_QUESTION_REQUEST,
		GET_ASSIGNMENT_CHARTS_QUESTION_SUCCESS,
		GET_ASSIGNMENT_CHARTS_QUESTION_FAILURE,

		GET_ASSIGNMENT_CHARTS_PERIOD_REQUEST,
		GET_ASSIGNMENT_CHARTS_PERIOD_SUCCESS,
		GET_ASSIGNMENT_CHARTS_PERIOD_FAILURE,

		GET_ASSIGNMENT_CHARTS_TIMEROUND_REQUEST,
		GET_ASSIGNMENT_CHARTS_TIMEROUND_SUCCESS,
		GET_ASSIGNMENT_CHARTS_TIMEROUND_FAILURE,

		SET_ASSIGNMENT_DETAIL_TABLE_CURRENT_PAGEINDEX,
		SET_ASSIGNMENT_DETAIL_TABLE_PAGEINDEX,
		GET_ASSIGNMENT_DETAIL_TABLE_REQUEST,
		GET_ASSIGNMENT_DETAIL_TABLE_SUCCESS,
		GET_ASSIGNMENT_DETAIL_TABLE_FAILURE,
		} from '../actions/homeWorkManagement'
import cloneDeep from 'lodash/cloneDeep';
import merge from 'lodash/merge';
/**
 * 初始作业管理页面状态
 * @type {Object}
 */
export const INITIALIZE_STATE = {
	// 提示消息
	message:'',
	// 默认二级视图(布置作业,批改作业)
	view: ASSIGNMENT,
	//布置作业概况
	assignmentTable:{
		isFetching:false,
		dataSource:[[]]
	},
	correctionTable:{
		isFetching:false,
		dataSource:[[]]
	},
	assignmentCharts:{
		//各种表
		frequency:{
			isFetching:false,
			dataSource:[[],[],[],[]]
		},
		average:{
			isFetching:false,
			dataSource:[[],[],[],[]]
		},
		difficulty:{
			isFetching:false,
			dataSource:[[],[],[]]
		},
		question:{
			isFetching:false,
			dataSource:[]
		},
		period:{
			isFetching:false,
			dataSource:[[],[],[],[]]
		},
		timeround:{
			isFetching:false,
			dataSource:[[],[],[],[]]
		}
	},
	assignmentDetailTable:{
		isFetching:false,
		//总页数
		pageIndex:0,
		//当前页码
		currentPageIndex:1,
		dataSource:[[]]
	},
	correctionDetailTable:{
		isFetching:false,
		//总页数
		pageIndex:0,
		//当前页码
		currentPageIndex:1,

		dataSource:[[]]
	}
	
};
/**
 * 处理基础
 * @param  {[type]} state  [description]
 * @param  {Object} action [description]
 * @return {[type]}        [description]
 */
export default function homeWorkManagement(state = INITIALIZE_STATE, action = {}) {
	let { type, ...others } = action;
	switch(type) {
		/**
		 * 切换一级试图
		 * @type {[type]}
		 */
		case SWITCH_HOME_WORK_VIEW :
		{
			let { view } = others.payload;
			let newState = Object.assign({},state,{
				view
			})
			return newState;
		};
		/**
		 * 异步请求布置作业概况
		 * @type {[type]}
		 */
		case GET_ASSIGNMENT_TABLE_REQUEST:
		{
			return merge({},state,{
				assignmentTable:{
					isFetching:true
				}
			})
		}
		case GET_ASSIGNMENT_TABLE_SUCCESS:
		{
			let { dataSource } = others.payload;
			let assignmentTable = cloneDeep(state.assignmentTable);
				Object.assign(assignmentTable,{
					dataSource,
					isFetching:false
				})
			return Object.assign({},state,{
				assignmentTable
			})
		};
		case GET_ASSIGNMENT_TABLE_FAILURE:
		{
			let { errorMessage } = others.payload;
			return merge({}, state, {
						assignmentTable: {
							isFetching: false
						},
						message:errorMessage
					})
		};
		/**
		 * 布置作业图表周布置频次
		 * @type {[type]}
		 */
		case GET_ASSIGNMENT_CHARTS_FREQUENCY_REQUEST:
		{
			return merge({}, state, {
					assignmentCharts: {
						frequency: {
							isFetching: true
						}
					}
				});
		};
		case GET_ASSIGNMENT_CHARTS_FREQUENCY_SUCCESS:
		{
			let { dataSource } = others.payload;
			let assignmentCharts = cloneDeep(state.assignmentCharts);
			let frequency = cloneDeep(assignmentCharts.frequency);
				Object.assign(frequency,{
					isFetching:false,
					dataSource
				});
				Object.assign(assignmentCharts,{
					frequency
				});
			return	Object.assign({}, state, {
						assignmentCharts
					})
		};
		case GET_ASSIGNMENT_CHARTS_FREQUENCY_FAILURE:
		{
			let { errorMessage } = others.payload;
			return merge({}, state, {
					message:errorMessage,
					assignmentCharts: {
						frequency: {
							isFetching: false
						}
					}
				});
		};
		/**
		 * 布置作业图表平均作业量
		 * @type {[type]}
		 */
		case GET_ASSIGNMENT_CHARTS_AVERAGE_REQUEST:
		{
			return	merge({}, state, {
					assignmentCharts: {
						average: {
							isFetching: true
						}
					}
				});
		};
		case GET_ASSIGNMENT_CHARTS_AVERAGE_SUCCESS:
		{
			let { dataSource } = others.payload;
			let assignmentCharts = cloneDeep(state.assignmentCharts);
			let average = cloneDeep(assignmentCharts.average);
			Object.assign(average,{
					isFetching:false,
					dataSource
				});
				Object.assign(assignmentCharts,{
					average
				});
			return	Object.assign({}, state, {
						assignmentCharts
					});
		};
		case GET_ASSIGNMENT_CHARTS_AVERAGE_FAILURE:
		{
			let { errorMessage } = others.payload;
			return merge({}, state, {
					message: errorMessage,
					assignmentCharts: {
						average: {
							isFetching: false
						}
					}
				});
		};
		/**
		 * 布置作业图表难度分布
		 * @type {[type]}
		 */
		case GET_ASSIGNMENT_CHARTS_DIFFICULTY_REQUEST:
		{
			return	merge({}, state, {
					assignmentCharts: {
						difficulty: {
							isFetching: true
						}
					}
				});
		};
		case GET_ASSIGNMENT_CHARTS_DIFFICULTY_SUCCESS:
		{
			let { dataSource } = others.payload;
			let assignmentCharts = cloneDeep(state.assignmentCharts);
			let difficulty = cloneDeep(assignmentCharts.difficulty);
				Object.assign(difficulty,{
					isFetching:false,
					dataSource
				});
				Object.assign(assignmentCharts,{
					difficulty
				});
			return	Object.assign({}, state, {
						assignmentCharts
					});
		};
		case GET_ASSIGNMENT_CHARTS_DIFFICULTY_FAILURE:
		{
			let { errorMessage } = others.payload;
			return merge({}, state, {
					message: errorMessage,
					assignmentCharts: {
						difficulty: {
							isFetching: false
						}
					}
				});
		};
		/**
		 * 布置作业图表题型分布
		 * @type {[type]}
		 */
		case GET_ASSIGNMENT_CHARTS_QUESTION_REQUEST:
		{
			return	merge({}, state, {
					assignmentCharts: {
						question: {
							isFetching: true
						}
					}
				});
		};
		case GET_ASSIGNMENT_CHARTS_QUESTION_SUCCESS:
		{
			let { dataSource } = others.payload;
			let assignmentCharts = cloneDeep(state.assignmentCharts);
			let question = cloneDeep(assignmentCharts.question);
				Object.assign(question,{
					isFetching:false,
					dataSource
				});
				Object.assign(assignmentCharts,{
					question
				});
			return	Object.assign({}, state, {
						assignmentCharts
					});
		};
		case GET_ASSIGNMENT_CHARTS_QUESTION_FAILURE:
		{
			let { errorMessage } = others.payload;
			return merge({}, state, {
					message: errorMessage,
					assignmentCharts: {
						question: {
							isFetching: false
						}
					}
				});
		};
		/**
		 * 布置作业图表布置周期
		 * @type {[type]}
		 */
		case GET_ASSIGNMENT_CHARTS_PERIOD_REQUEST:
		{
			return	merge({}, state, {
					assignmentCharts: {
						period: {
							isFetching: true
						}
					}
				});
		};
		case GET_ASSIGNMENT_CHARTS_PERIOD_SUCCESS:
		{
			let { dataSource } = others.payload;
			let assignmentCharts = cloneDeep(state.assignmentCharts);
			let period = cloneDeep(assignmentCharts.period);
				Object.assign(period,{
					isFetching:false,
					dataSource
				});
				Object.assign(assignmentCharts,{
					period
				});
			return	Object.assign({}, state, {
						assignmentCharts
					});
			
		};
		case GET_ASSIGNMENT_CHARTS_PERIOD_FAILURE:
		{
			let { errorMessage } = others.payload;
			return merge({}, state, {
					message: errorMessage,
					assignmentCharts: {
						period: {
							isFetching: false
						}
					}
				});
		};
		/**
		 * 布置作业图表年级tab
		 * @type {[type]}
		 */
		case GET_ASSIGNMENT_CHARTS_TIMEROUND_REQUEST:
		{
			return	merge({}, state, {
					assignmentCharts: {
						timeround: {
							isFetching: true
						}
					}
				});
		};
		case GET_ASSIGNMENT_CHARTS_TIMEROUND_SUCCESS:
		{
			let { dataSource } = others.payload;
			let assignmentCharts = cloneDeep(state.assignmentCharts);
			let timeround = cloneDeep(assignmentCharts.timeround);
				Object.assign(timeround,{
					isFetching:false,
					dataSource
				});
				Object.assign(assignmentCharts,{
					timeround
				});
			return	Object.assign({}, state, {
						assignmentCharts
					});
		};
		case GET_ASSIGNMENT_CHARTS_TIMEROUND_FAILURE:
		{
			let { errorMessage } = others.payload;
			return merge({}, state, {
					message: errorMessage,
					assignmentCharts: {
						timeround: {
							isFetching: false
						}
					}
				});
		};
		/**
		 * 布置作业数据详情
		 * @type {[type]}
		 */
		case GET_ASSIGNMENT_DETAIL_TABLE_REQUEST:
		{
			return	merge({}, state, {
					assignmentDetailTable: {
						isFetching:true
					}
				});
		};
		case GET_ASSIGNMENT_DETAIL_TABLE_SUCCESS:
		{
			let { dataSource } = others.payload;
			let assignmentDetailTable = cloneDeep(state.assignmentDetailTable);
				Object.assign(assignmentDetailTable,{
					isFetching:false,
					dataSource
				})
			return	Object.assign({}, state, {
						assignmentDetailTable
					});
		};
		case GET_ASSIGNMENT_DETAIL_TABLE_FAILURE:
		{
			let { errorMessage } = others.payload;
			return merge({}, state, {
					message: errorMessage,
					assignmentDetailTable: {
						isFetching:false
					}
				});
		};
		case SET_ASSIGNMENT_DETAIL_TABLE_CURRENT_PAGEINDEX:
		{
			let { currentPageIndex } = others.payload;
			return merge({},state,{
					assignmentDetailTable:{
						currentPageIndex
				}
			});
		};
		case SET_ASSIGNMENT_DETAIL_TABLE_PAGEINDEX:
		{
			let { pageIndex } = others.payload;
			return merge({},state,{
					assignmentDetailTable:{
						pageIndex
				}
			});
		};
		default: 
			return state;
	}
};